unit UsuarioDM;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, FileUtil, LResources, Forms, Controls, Graphics,
  Dialogs, ZDataset, ZSqlUpdate, MainDM, QueryConditions, md5;

type

  { TUsuarioData }

  TUsuarioData = class(TMainDataModule)
    FuncDatasource: TDatasource;
    FuncZQuerydesc_acao: TStringField;
    FuncZQuerydesc_funcionalidade: TStringField;
    FuncZQueryid_funcionalidade: TLargeintField;
    FuncZQueryid_funcionalidade_modulo: TLargeintField;
    UsuarioAuxZQueryid_perfil: TLargeintField;
    UsuarioData: TDatasource;
    UsuarioDataSource: TDatasource;
    UsuarioAuxDataSource: TDatasource;
    UsuarioAuxZQuery: TZQuery;
    UsuarioZQueryativo: TStringField;
    UsuarioZQueryativo1: TStringField;
    UsuarioZQueryid_perfil: TLargeintField;
    UsuarioZQueryid_pessoa: TLargeintField;
    UsuarioZQueryid_pessoa1: TLargeintField;
    UsuarioZQueryid_usuario: TLargeintField;
    UsuarioZQueryid_usuario1: TLargeintField;
    UsuarioZQuerylogin: TStringField;
    UsuarioZQuerylogin1: TStringField;
    UsuarioZQuerynome_pessoa: TStringField;
    UsuarioZQuerynome_pessoa1: TStringField;
    UsuarioZQuerysenha: TStringField;
    UsuarioZQuerysenha1: TStringField;
    UsuarioZUpdate: TZUpdateSQL;
    UsuarioZQuery: TZQuery;
    FuncZQuery: TZQuery;
    procedure DataModuleCreate(Sender: TObject);
    /// Abre o DataSet
    procedure OpenDataSet(); override;
    /// Fecha o DataSet
    procedure CloseDataSet(); override;
    /// Realiza busca
    /// @param(Condicoes são as condições para a busca)
    /// @param(Tabela é a tabela no banco de dados)
    procedure Search(Condicoes: TQueryConditions; Tabela: string); override;
    function VerificaSenha(Login: string; Senha: string): integer;
    function EstaAtivo(IDUsuario: integer): boolean;
    procedure InsereValores(Senha: string);
    procedure HabilitaAcoes(IDUsuario: integer);
    procedure BuscaUsuarios();
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  UsuarioData: TUsuarioData;

implementation

uses ConnectionDM, MainFm, ActnList;

{ TUsuarioData }

procedure TUsuarioData.DataModuleCreate(Sender: TObject);
begin
  MainDataSet := UsuarioZQuery;
end;

procedure TUsuarioData.OpenDataSet();
begin
  ConnectionData.EmptyQuery(UsuarioZQuery, 'usuario');
   BuscaUsuarios();
end;

procedure TUsuarioData.CloseDataSet();
begin
  UsuarioAuxZQuery.Open();
  UsuarioZQuery.Close();
end;

procedure TUsuarioData.Search(Condicoes: TQueryConditions; Tabela: string);
begin
  ConnectionData.OpenQueryConditions(UsuarioZQuery, Tabela, Condicoes);
end;

function TUsuarioData.VerificaSenha(Login: string; Senha: string): integer;
var
  senhaCrip: string;
begin
  senhaCrip := MD5Print(MD5String(Senha));
  with UsuarioZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('Select * FROM usuario');
    SQL.Add('where login =:login AND senha =:senha');
    ParamByName('login').AsString := Login;
    ParamByName('Senha').AsString := senhaCrip;
    Open();
    if (RecordCount > 0) then begin
      Result := FieldByName('id_usuario').AsInteger;
    end else begin
      Result := 0;
    end;
  end;
end;

function TUsuarioData.EstaAtivo(IDUsuario: integer): boolean;
begin
  with UsuarioAuxZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('Select ativo FROM usuario');
    SQL.Add('WHERE id_usuario=:id_usuario');
    ParamByName('id_usuario').AsInteger := IDUsuario;
    Open();
    if RecordCount > 0 then begin
      if FieldByName('ativo').AsString = 'A' then begin
        Result := True;
      end else
        Result := False;
    end else
      Result := False;
  end;
end;

procedure TUsuarioData.InsereValores(Senha: string);
begin
  with UsuarioZQuery do begin
    FieldByName('senha').AsString := Senha;
    Post();
  end;
  UsuarioAuxZQuery.Refresh();
end;

procedure TUsuarioData.HabilitaAcoes(IDUsuario: integer);
var
  ActionL: TActionList;
  Nome: string;
  I, J: integer;
begin
  //For para as Ações com tamanho de imagens de 32X32
  for I := 0 to MainForm.PrincipalActionList.ActionCount - 1 do begin
    TAction(MainForm.PrincipalActionList.Actions[I]).Enabled := False;
  end;
  //For para as Ações dos modulos e imagens de tamanho 48X48
  for I := 0 to MainForm.ModulosActionList.ActionCount - 1 do begin
    TAction(MainForm.ModulosActionList.Actions[I]).Enabled := False;
  end;
  {
  for I := 0 to MainForm.ComponentCount - 1 do begin
    if MainForm.Components[I] is TActionList then begin
      ActionL:=TActionList(MainForm.Components[I]);
      ActionL.State:=asSuspended;
      for I := 0 to ActionL.ActionCount - 1 do begin
        TAction(ActionL.Actions[i]).Enabled := False;
      end;
    end;
  end;}
  with FuncZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('SELECT f.desc_acao FROM perfil_funcionalidade as pf, funcionalidade as f, usuario as u');
    SQL.Add('WHERE pf.id_funcionalidade = f.id_funcionalidade AND pf.id_perfil = u.id_perfil AND');
    SQL.Add('u.id_usuario=:id_usuario');
    ParamByName('id_usuario').AsInteger := IDUsuario;
    Open();
  end;
  for J := 1 to FuncZQuery.RecordCount do begin
    FuncZQuery.RecNo := J;
    Nome := FuncZQuery.FieldByName('desc_acao').AsString;
    //For para as Ações com tamanho de imagens de 32X32
    for I := 0 to MainForm.PrincipalActionList.ActionCount - 1 do begin
      if TAction(MainForm.PrincipalActionList.Actions[I]).Name = Nome then begin
        TAction(MainForm.PrincipalActionList.Actions[I]).Enabled := True;
      end;
    end;
    //For para as Ações dos modulos e imagens de tamanho 48X48
    for I := 0 to MainForm.ModulosActionList.ActionCount - 1 do begin
      if TAction(MainForm.ModulosActionList.Actions[I]).Name = Nome then begin
        TAction(MainForm.ModulosActionList.Actions[I]).Enabled := True;
      end;
    end;
  end;
end;

procedure TUsuarioData.BuscaUsuarios();
begin
   with UsuarioAuxZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('SELECT p.nome_pessoa,p.id_pessoa, u.*');
    SQL.Add('FROM pessoa as p, usuario as u');
    SQL.Add('WHERE p.id_pessoa = u.id_usuario');
    Open();
   end;
end;

initialization
  {$I usuariodm.lrs}
end.



